﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using DotNet.Business;
using DotNet.Common;
using DotNet.Model;
using System.Collections.Generic;
public partial class Admin_TabAdmin : System.Web.UI.Page
{  
    productBll pBll = new productBll();
    ProductTabBll ptBll = new ProductTabBll();
    private static readonly object objsys = new object();
    public string pname = "-/-";
    public string TabTitle = "";
    public string TabContent = "";
    protected void Page_Load(object sender, EventArgs e)
    {
   
        if (!IsPostBack)
        {
            if (Request.QueryString["TabId"] != null)
            {
                AjaxTabContent(Convert.ToInt32(Request.QueryString["TabId"]));
            }
            if (Request.QueryString["productid"] != null)
            {                
                this.ProductID.Value = Request.QueryString["productid"].ToString();
                product productModel = pBll.GetModel(Convert.ToInt32(Request.QueryString["productid"]));
                Session["FCKeditor:UserFilesPath"] = "~/UploadFiles/productUpload/" + productModel.Productmodel;
                this.productName_span.InnerText =  productModel.productName;
                BindTab(Convert.ToInt32(this.ProductID.Value));
            }
            if (Request.QueryString["state"] != null)
            {
                string state = Request.QueryString["state"].ToString();
                switch (state.Trim())
                {
                    case "insert":
                        Insert();
                        break;
                    case "del":
                        Delete();
                        break;
                    case "edit":
                        edit();
                        break;
                    default:
                        break;
                }
            }   
        }
    }

    public void edit() 
    {
        int tid = Convert.ToInt32(Request.QueryString["tid"]);
        ProductTab pt = ptBll.GetModel(tid);
        this.TabTitle_txt.Value = pt.tabTitle;
        this.TabSort.Value = pt.tabSort.ToString();
        this.FCKeditor1.Value = pt.tabContent;
    }

    public void Delete() 
    {
        int tid = Convert.ToInt32(Request.QueryString["tid"]);
        if (ptBll.Delete(tid))
        {
            Response.Write("1");
        }
        else 
        {
            Response.Write("删除错误！");
        }
        Response.End();
    }

    private void AjaxTabContent(int tid)
    {
        lock (objsys)
        {
            ProductTab pt = ptBll.GetModel(tid);
            Response.Write(pt.tabContent);
            Response.End();
        }
    }

    private void BindTab(int pid)
    {
        StringBuilder sb = new StringBuilder();
        IList<ProductTab> ListTab = ptBll.GetModelByProId(pid);
        if (ListTab.Count>0)
        {
            for (int i = 0; i < ListTab.Count; i++)
            {
                if (i == 0)
                {
                    sb.AppendFormat("<li class=\"currentBtn\" title=\"" + ListTab[i].tabID + "\"><a href=\"javascript:void(0)\" onclick=\"tab({0},{2})\"><div style=\"width:70px; float:left;\">{1}</div><div style=\"width:15px; float:right; margin-top:8px; margin-right:5px; \"><img src=\"imgs/20100113132707504.gif\" style=\"cursor:pointer\" onclick=\"del(" + ListTab[i].tabID + ")\"  border=\"0\" /></div></a></li>", ListTab[i].tabID, ListTab[i].tabTitle, i);
                    ProductTab pt = ptBll.GetModel(ListTab[i].tabID);
                    TabContent = "<div class=\"tabCot\" id='" + i + "' title='0'> " + pt.tabContent + "</div>";
                }
                else
                {
                    sb.AppendFormat("<li title=\"" + ListTab[i].tabID + "\"><a href=\"javascript:void(0)\" onclick=\"tab({0},{2})\"><div style=\"width:70px; float:left;\">{1}</div><div style=\"width:15px; float:right; margin-top:8px; margin-right:5px; \"><img src=\"imgs/20100113132707504.gif\" style=\"cursor:pointer\" onclick=\"del(" + ListTab[i].tabID + ")\"  border=\"0\" /></div></a></li>", ListTab[i].tabID, ListTab[i].tabTitle, i);
                    TabContent += "<div class=\"tabCot\" id='" + i + "' title='0'  style=\"display:none\"  >正在加载...</div>";
                }
            }
            TabTitle = sb.ToString();
           
        }
       
    }

    private void Insert()
    {
        ProductTab pt = new ProductTab();
        pt.productID = Convert.ToInt32(Request.Form["productid"]);
        pt.tabTitle = Request.Form["TabTitle_txt"].ToString();
        if (pt.tabTitle == "")
        {
            Response.Write("tab标题不能为空！");
            Response.End();
        }
        if (Request.Form["TabSort"]=="")
        {
            Response.Write("tab顺序不能为空！");
            Response.End();
        }
        if (!CommUtil.IsNumStr(Request.Form["TabSort"]))
        {
            Response.Write("tab顺序必须为数字！");
            Response.End();
        }
        pt.tabSort = Convert.ToInt32(Request.Form["TabSort"]);
        if (Request.Form["FCKeditor1"]=="")
        {
            Response.Write("tab内容不能为空！");
            Response.End();
        }
        pt.tabContent = Server.HtmlDecode(Request.Form["FCKeditor1"].ToString());
        CommUtil.ValidID(pt.tabSort.ToString());       
        if (ptBll.Insert(pt))
        {
            Response.Write("1");            
        }
        else
        {
            Response.Write("插入错误！！");
        }
        Response.End();
    }
}
